<?php


namespace app\admin\model;


use app\admin\validate\AdminUser;
use think\Validate;

class UserModel extends Common
{
    // 设置字段信息
    protected $schema = [

    ];
    /**
     * 类型转换
     * @var string[]
     */
    protected $type = [

    ];
    /**
     * @var string
     */
    protected $name = 'admin_user';

    protected $defaultSoftDelete = 0;

    protected $autoWriteTimestamp = true;

    /**
     * 驼峰格式转换
     * @var bool
     */
    protected $convertNameToCamel = true;

    /**
     * 创建系统用户
     * @author ran
     * @date 2021-10-22 16:57
     * mailbox 466180170@qq.com
     */
    public function createAdminUserResource($request)
    {
        $validate = new AdminUser();
        if (!$validate->scene('create')->check($this->humpToUnderscore($request))) {
            $this->error = $validate->getError();
            return false;
        }
        if (empty($request['groupId'])) {
            $this->error = '请选择系统角色';
            return false;
        }
        try {
            $res =$this->where('username',$request['username'])->where('type',1)->find();
            if($res){
                $this->error = '系统账户手机号不能重复,请更换手机号';
                return false;
            }
            $salt = substr(md5(time()), 0, 4);
            $data = [
                'username' => $request['username'],
                'realname' => $request['realname'],
                'salt' => $salt,
                'password' => user_md5($request['password'] ?? '123456', $salt, $request['username']),
                'type' => 1,
                'group_name'=>$request['groupName']??'',
                'group_id'=>$request['groupId'],
                'sex' => $request['sex'] ?? '男',
                'mobile' => $request['username'],
                'structure_name' => $request['structureName'],
                'structure_id' => $request['structureId'],
            ];
            $res = $this->create($data);
            if ($res->id) {
                $accessModel = new Access();
                $accessModel->create(['user_id' => $res->id, 'group_id' => $request['groupId']]);
                return true;
            }
            $this->error = '系统用户资源入库失败';
            return false;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }

    /**
     * @param $request
     * @author ran
     * @date 2021-11-21 19:50
     * mailbox 466180170@qq.com
     */
    public function updateAdminUserResource($request)
    {
        $validate = new AdminUser();
        if (!$validate->scene('update')->check($this->humpToUnderscore($request))) {
            $this->error = $validate->getError();
            return false;
        }
        if (empty($request['groupId'])) {
            $this->error = '请选择系统角色';
            return false;
        }
        if (empty($request['id'])) {
            $this->error = '缺少更新主键id';
            return false;
        }
        try {
            $salt = substr(md5(time()), 0, 4);
            $data = [
                'id' =>$request['id'],
                'username' => $request['username'],
                'realname' => $request['realname'],
                'group_name'=>$request['groupName']??'',
                'group_id'=>$request['groupId'],
                'sex' => $request['sex'] ?? '男',
                'mobile' => $request['username'],
                'structure_name' => $request['structureName'],
                'structure_id' => $request['structureId'],
            ];
            if(!empty($request['password'])){
                $data['salt'] =$salt;
                $data['password'] =user_md5($request['password'], $salt, $request['username']);
            }
            $this->update($data,['id'=>$request['id']]);
            $accessModel = new Access();
            $accessModel->update(['group_id' => $request['groupId']],['user_id'=>$request['id']]);
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }
    /**
     * @author ran
     * @date 2021-11-20 19:16
     * mailbox 466180170@qq.com
     */
    public function readAdminUserList($request,$page,$limit)
    {
        $where=[];
        $username = isset($request['username']) && trim($request['username']) ? trim($request['username']) : '';
        $username && array_push($where, ['username', '=', $username]);
        $connect = $this->where('type',1)
            ->where($where)
            ->where('id','<>',1);
        $count =$connect->count();
        $list = $connect->page($page, $limit)->select();
        return [
            'list' => $list,
            'dataCount' => $count
        ];

    }
    /**
     * @author ran
     * @date 2022-01-19 10:14
     * mailbox 466180170@qq.com
     */
    public function updateAdminUserStatus($id,$status){
        $data=[
            'status'=>$status
        ];
        $this->update($data,['id'=>$id]);
        return true;
    }
    /**
     * @author ran
     * @date 2021-11-20 19:16
     * mailbox 466180170@qq.com
     */
    public function readCustomerAdminUserList($request,$page,$limit)
    {
        $where = [];
        $name = isset($request['name']) && trim($request['name']) ? trim($request['name']) : '';
        $name && array_push($where, ['realname', 'like', '%' . $name . '%']);
        $connect = $this->where($where)->where('type',1)->where('structure_id',8);
        $count =$connect->count();
        $list = $connect->page($page, $limit)->select();
        return [
            'list' => $list,
            'dataCount' => $count
        ];

    }

}